package com.ledim.widget.ptr;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import com.letv.android.young.client.R;

/**
 * Created by ccx on 16/05/18.
 * 乐次元Listview状态View
 */
public class LedimListViewStateView extends FrameLayout {

    public static final byte STATE_NORMAL = 0;
    public static final byte STATE_NO_NET = 1;
    public static final byte STATE_SERVER_ERROR = 2;
    public static final byte STATE_NO_DATA = 4;
    public static final byte STATE_LOADING = 5;
    /**
     * 其他状态，如ptr的refresh与加载更多，效果
     */
    public static final byte STATE_OTHER = 7;

    private byte state;

    private View ll_state_view;
    private ImageView iv_hint;
    private TextView tv_hint;
    private View ll_loading;

    private int imgNoNetId, imgNoDataId, imgSEId;
    private int hintNoNetId, hintNoDataId, hintSEId;
    private AnimationDrawable animationDrawable = null;

    public LedimListViewStateView(Context context) {
        super(context);
        initView();
    }

    /**
     * @param textId
     *         设置无数据文案
     */
    public void setNoDataText(int textId) {
        if (textId != 0) {
            hintNoDataId = textId;
        }
    }

    public LedimListViewStateView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public LedimListViewStateView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    /**
     * 初始化资源
     */
    private void initRes() {
        imgNoNetId = R.drawable.img_no_net;
        imgSEId = imgNoDataId = R.drawable.img_no_data;
        hintNoNetId = R.string.str_no_net;
        hintSEId = R.string.str_server_error;
        hintNoDataId = R.string.str_nodata_error;
    }

    private void initView() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.lib_state_view, this, false);
        ll_loading = view.findViewById(R.id.ll_loading);
        // loading_text.setVisibility(GONE);
        ImageView loadingImage = (ImageView) view.findViewById(R.id.progress_view);
        loadingImage.setImageResource(R.anim.loading_animation);
        animationDrawable = (AnimationDrawable) loadingImage.getDrawable();
        animationDrawable.setOneShot(false);
        ll_state_view = view.findViewById(R.id.ll_state_view);
        iv_hint = (ImageView) ll_state_view.findViewById(R.id.iv_stateview_icon);
        tv_hint = (TextView) ll_state_view.findViewById(R.id.tv_stateview_hint);
        addView(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        initRes();
        setState(STATE_NORMAL);
    }


    public int getState() {
        return state;
    }

    public void setState(byte state) {
        this.state = state;
        switch (state) {
            case STATE_NO_NET:
                showStateView(imgNoNetId, hintNoNetId);
                break;
            case STATE_SERVER_ERROR:
                showStateView(imgSEId, hintSEId);
                break;
            case STATE_NO_DATA:
                showStateView(imgNoDataId, hintNoDataId);
                break;
            // case STATE_LOADING:
            // showStateView(-1, -1, -1);
            // break;
            default:
                showStateView(-1, -1);
                break;
        }
    }

    private void showStateView(int imgId, int hintId) {
        if (animationDrawable != null) {
            animationDrawable.stop();
        }
        if (state == STATE_NORMAL) {
            ll_state_view.setVisibility(GONE);
            ll_loading.setVisibility(GONE);
        } else if (state == STATE_OTHER) {
            ll_state_view.setVisibility(GONE);
            ll_loading.setVisibility(GONE);
        } else if (state == STATE_LOADING) {
            ll_state_view.setVisibility(GONE);
            if (animationDrawable != null) {
                animationDrawable.start();
            }
            ll_loading.setVisibility(VISIBLE);
        } else {
            ll_loading.setVisibility(GONE);
            ll_state_view.setVisibility(VISIBLE);
            if (imgId != -1) {
                iv_hint.setImageResource(imgId);
                iv_hint.setVisibility(VISIBLE);
            } else {
                iv_hint.setVisibility(GONE);
            }
            if (hintId == -1) {
                tv_hint.setVisibility(GONE);
            } else {
                tv_hint.setText(hintId);
                tv_hint.setVisibility(VISIBLE);
            }
        }

    }

    public void setOnRetryListener(OnClickListener onRetryListener) {
        ll_state_view.setOnClickListener(onRetryListener);
    }
}
